package com.cx.mall.goods.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.cx.mall.goods.mapper.CategoryBrandMapper;
import com.cx.mall.common.model.goods.Brand;
import com.cx.mall.goods.mapper.BrandMapper;
import com.cx.mall.common.model.goods.CategoryBrand;
import com.cx.mall.goods.service.IBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.mall.goods.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author lyt
 * @since 2025-07-10
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;
    @Autowired
    private ICategoryService categoryService;

    @Override
    public List<Brand> listByOneCategoryId(int id) {
        // 1 获取一级分类下的二级类别Id
        List<Integer> twoIds = categoryService.listByParentId(id);
        // 2 获取二级类别下的三级类别Id
        List<Integer> threeIds = categoryService.listByParentId(twoIds);
        // 3 获取三级分类下的品牌id
        List<Integer> ids = listBrandIdsByCategoryIds(threeIds);
        // 4 查询品牌信息
        List<Brand> list = listByIds(ids);
        return list;
    }
    @Override
    public List<Brand> listByTwoCategoryId(int id) {
        // 1 获取二级类别下的三级类别Id
        List<Integer> threeIds = categoryService.listByParentId(id);
        // 2 获取三级分类下的品牌id
        List<Integer> ids = listBrandIdsByCategoryIds(threeIds);
        // 3 查询品牌信息
        List<Brand> list = listByIds(ids);
        return list;
    }

    @Override
    public List<Brand> listByThreeCategoryId(int id) {
        List<Integer> ids = categoryService.listByParentId(id);
        //查询品牌列表
        List<Brand> list = listByIds(ids);
        return list;
    }

    /**
     * 根据品牌ID查询品牌列表
     * @param ids
     * @return
     */
    private List<Brand> listByIds(List<Integer> ids) {
        // 2 查询品牌信息
        // 2.1 创建条件构建器
        LambdaQueryChainWrapper<Brand> bradWrapper = new LambdaQueryChainWrapper<>(baseMapper);
        List<Brand>list =bradWrapper
                .in(Brand::getId,ids)
                .select(Brand::getId,Brand::getLogo)
                .list();
        return list;
    }
    /**
     * 根据分类id查询品牌id
     * @param ids
     * @return
     */
    private List<Integer> listBrandIdsByCategoryIds(List<Integer> ids) {
        // 1 获取三级分类下的品牌id
        // 1.1 创建条件构建器
        LambdaQueryChainWrapper<CategoryBrand> wrapper =
                new LambdaQueryChainWrapper<>(categoryBrandMapper);
        // 1.2 查询三级分类下的品牌id
        List<Integer> brandIds = wrapper.in(CategoryBrand::getCategoryId, ids)
                .select(CategoryBrand::getBrandId)
                .list()
                .stream()
                .map(CategoryBrand::getBrandId)
                .distinct()
                .collect(Collectors.toList());
        return brandIds;
    }
}
